/**
 * Created by Richard Radlherr.
 * Date: 10.06.2006 19:00
 */

using System;
using TagTool.Core;
using TagTool.GraphicTypes;

namespace TagTool.Nodes.Misc
{

	public class Counter : BaseNode
	{
		private InputSlot start;
		private InputSlot stop;
		private InputSlot step;
		
		private InputSlot stepUp;
		private InputSlot stepDown;

		//private InputSlot startStop;
		
		private OutputSlot current;
		
		bool up = false;
		bool down = false;
        bool trigger;

		public Counter() : base()
		{
			Name = "Counter";
			start = new InputSlot(this, "Start", 0.0);
			stop = new InputSlot(this, "Stop", 100.0);
			step = new InputSlot(this, "Step", 1.0);
			
			stepUp = new InputSlot(this, "Step up", false);
			stepDown = new InputSlot(this, "Step down", false);

			//startStop = new InputSlot(this, "Start/Stop", false);

			current = new OutputSlot(this, "Current", typeof(double));
			current.Value = 0.0;
			
			start.ParseCallback = ParseHelper.ParseDouble;
			stop.ParseCallback = ParseHelper.ParseDouble;
			step.ParseCallback = ParseHelper.ParseDouble;
		}

		[NodeAttribute(SlotType.Input, Show = true)]
		public InputSlot Start
		{
			get { return start; }
		}

		[NodeAttribute(SlotType.Input, Show = true)]
		public InputSlot Stop
		{
			get { return stop; }
		}
		
		[NodeAttribute(SlotType.Input, Show = true)]
		public InputSlot Step
		{
			get { return step; }
		}
		
		[NodeAttribute(SlotType.Input, Show = true)]
		public InputSlot StepDown
		{
			get { return stepDown; }
		}
		
		[NodeAttribute(SlotType.Input, Show = true)]
		public InputSlot StepUp
		{
			get { return stepUp; }
		}
		
		[NodeAttribute(SlotType.Output, Show = true)]
		public OutputSlot Current
		{
			get { return current; }
		}
		
//		[NodeAttribute(SlotType.Input, Show = true)]
//		public InputSlot StartStop
//		{
//			get { return startStop; }
//		}

		public override void Update(Object sender)
		{
//			if ((bool)startStop.Value)
//			{
				if ((bool)stepDown.Value && !down)
				{
					double v = (double)current.Value - (double)step.Value;
					
					if (v > (double)stop.Value || v < (double)start.Value)
					{
						current.Value = stop.Value;
					}
					else
					{
						current.Value = v;
					}
					down = true;
					CastTrigger();
				}
				else if (!(bool)stepDown.Value && down)
				{
					down = false;
				}
				else if ((bool)stepUp.Value && !up)
				{
					up = true;
					double v = (double)current.Value + (double)step.Value;
					
					if (v > (double)stop.Value || v < (double)start.Value)
					{
						current.Value = start.Value;
					}
					else
					{
						current.Value = v;
					}
					CastTrigger();
				}
				else if (!(bool)stepUp.Value && up)
				{
					up = false;
				}
				else if (trigger != (bool)TriggerIn.Value)
				{
                    trigger = !trigger;
					double v = (double)current.Value + (double)step.Value;
					
					if (v > (double)stop.Value || v < (double)start.Value)
					{
						current.Value = start.Value;
					}
					else
					{
						current.Value = v;
					}
					
					CastTrigger();
				}
				
				
//			}
//			else
//			{
//				current.Value = start.Value;
//			}
		}

	}
}
